<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        #box{
            height:100px;
            width:100px;
            background-color: burlywood;
            position: relative;
            left:0;
            top:0;
        }
    </style>
</head>
<body>
    <div id="box"></div>
    <script>
        let t=0,l=0;
        window.onkeydown=function(e){
            console.log(e.keyCode);
            switch (e.keyCode) {
                case 38:
                    t=t-10;
                    box.style.top=t+"px";
                    break;
                case 40:
                    t=t+10;
                    box.style.top=t+"px";
                    break;
                case 37:
                    l=l-10;
                    box.style.left=l+"px";
                    break;
                case 39:
                    l=l+10;
                    box.style.left=l+"px";
                    break;
                default:
                    alert("按键不正确，没有效果")
                    break;
            }
        }
    </script>
</body>
</html>